from qturboframe.public_ui.base_element import BaseElement
from qturboframe.lib.utils.check import CheckBasic
from qturboframe.lib.report.log import logger
from qturboframe.lib.webdriver.exceptions import SeleniumError
from qturboframe.lib.utils.CustomError.custom_error import CustomError


class TypeField(BaseElement, CheckBasic):
    '''hui文本框操作类'''

    def __init__(self, element_para, father=None, timeout=5):
        super().__init__(element_para, father, timeout)
        class_list = self._element.get_attribute('class').split(' ')
        if 'h-typefield' not in class_list:
            self._element = self._element.find_element_by_attribute(
                'xpath', './ancestor::div[contains(@class, "h-typefield")]', timeout=self.timeout)
        # 在ag-table中金额输入框中input框的class为.h-typefield-right
        self.type_field = self._element.find_element_by_attribute(
            'css selector', '.h-typefield-left,.h-typefield-right', timeout=self.timeout)

    def set_value(self, value):
        '''
        hui金额框填充

        :Args:
         - text: 填充值

        :Example:
         - 例: type_field.set_value('12345')
        '''
        self.type_field.focus()
        self.type_field.clear()
        self.type_field.send_keys(value)

    def is_default(self, value):
        """
        检查hui金额框件默认值

        :Args:
         - value: 待检查的值

        :Returns:
            检查匹配返回True，否则返回False

        :Example:
         - 例: type_field.is_default('12345')
        """
        self.type_field.focus()
        if self.type_field.text:
            text = self.type_field.text
        elif self.type_field.get_attribute('value'):
            text = self.type_field.get_attribute('value')
        elif self.type_field.get_attribute('placeholder'):
            text = self.type_field.get_attribute('placeholder')
        elif self.type_field.get_attribute('title'):
            text = self.type_field.get_attribute('title')
        else:
            text = ''
        if text == value:
            return True
        else:
            logger.error(CustomError.Error_EQ0302200_Type_Field_Default_Check,'文本框默认值不匹配！')
            return False

    def is_readonly(self):
        '''
        检查hui金额框是否只读

        :Return:
            只读返回True，否则返回False

        :Example:
         - 例: type_field.is_readonly()
        '''
        self.type_field.focus()
        return self.type_field.is_readonly()

    def is_nonnull(self):
        '''
        检查hui金额框是否有非空属性

        :Returns:
            非空返回True，否则返回False

        :Example:
         - 例: type_field.is_nonnull()
        '''
        self.type_field.focus()
        form_item = self._element.find_element_by_attribute(
            'xpath', './ancestor::div[contains(@class, "h-form-item-content")]/'
                     'parent::div[contains(@class, "h-form-item")]', timeout=self.timeout)
        item_class_list = form_item.get_attribute('class').split()
        if 'h-form-item-required' in item_class_list:
            return True
        else:
            return False

    def clear(self):
        '''
        hui金额框清空

        :Usage:
         - type_field.clear()
        '''
        self.type_field.focus()
        self.type_field.clear()

    def check_default(self, value):
        '''
        校验hui金额框默认值

        :Args:
         - value: 待检查的值

        :Raises:
            SeleniumError: 文本框默认值校验不通过

        :Example:
         - 例: type_field.check_default('admin')
        '''
        if not self.is_default(value):
            raise SeleniumError(CustomError.Error_EQ0302200_Type_Field_Default_Check,'文本框默认值校验不通过！')

    def check_readonly(self):
        '''
        校验hui金额框只读属性

        :Raises:
            SeleniumError: 文本框只读校验不通过

        :Example:
         - 例: type_field.check_readonly()
        '''
        if not self.is_readonly():
            raise SeleniumError(CustomError.Error_EQ0302201_Type_Field_Verify_Fail,'文本框只读校验不通过！')

    def check_nonnull(self):
        '''
        校验hui金额框非空属性

        :Raises:
            SeleniumError: 文本框非空校验不通过

        :Example:
         - 例: type_field.check_nonnull()
        '''
        if not self.is_nonnull():
            raise SeleniumError(CustomError.Error_EQ0302201_Type_Field_Verify_Fail,'文本框非空校验不通过！')
